using System.Linq;
using System.Reflection;

namespace TestMonkey.Models
{
	/// <summary>
	/// Metadata about a method or function.
	/// </summary>
	public class MethodModel
	{
		readonly MethodInfo m_Method;

		/// <summary>
		/// Initializes a new instance of the <see cref="MethodModel"/> class.
		/// </summary>
		/// <param name="method">The method.</param>
		public MethodModel(MethodInfo method)
		{
			m_Method = method;
		}

		/// <summary>
		/// This returns a name that can be used as an identifier.
		/// </summary>
		public CodeString SafeName
		{
			get
			{
				var parameters = m_Method.GetParameters();
				if (parameters.Count() == 0)
					return m_Method.Name;

				var list = from p in parameters select p.ParameterType.Name.Replace("<", "").Replace(">", "").Replace("`", "");
				return m_Method.Name + "_" + string.Join("_", list);
			}
		}

		/// <summary>
		/// Gets a value indicating whether this method returns a value.
		/// </summary>
		/// <value>
		/// <c>true</c> if this method returns a value; otherwise, <c>false</c>.
		/// </value>
		public bool HasReturn
		{
			get { return m_Method.ReturnType != typeof(void); }
		}

		/// <summary>
		/// Gets the signature of this method.
		/// </summary>
		/// <value>
		/// The signature.
		/// </value>
		public CodeString Signature
		{
			get
			{
				var parameters = m_Method.GetParameters();
				if (parameters.Count() == 0)
					return m_Method.Name + "()";

				var list = from p in parameters select p.ParameterType.Name + " " + p.Name;
				return m_Method.Name + "(" + string.Join(", ", list) + ")";
			}
		}

		/// <summary>
		/// Gets a value indicating whether this method is static.
		/// </summary>
		/// <value>
		///   <c>true</c> if this method is static; otherwise, <c>false</c>.
		/// </value>
		public bool IsStatic
		{
			get { return m_Method.IsStatic; }
		}
	}
}
